/**
 *	@project:	kybernetes
 *	@author:	D.Toepel
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */
package dt.htwk.core;


import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Hashtable;

import javax.swing.*;

import dt.htwk.extras.LanguageTools;
import dt.htwk.res.Resource;


/**
 * 
 * @author daniel
 *
 */
public class KCStartup {
	
	
	static KCClassLoader cl;
	private static FileChannel channel;
	private static FileLock lock;
	public static Boolean isDebugMode = false;
	
	public static void main(String[] args) {
		
		/*
		 * Laden der zusaetzlichen Bibliotheken
		 */
		cl = new KCClassLoader(args);
		
		
		/*
		 * Programm mittels Dateisperre vor mehrfachen Ausfuehren schuetzen
		 */
		File lockFile = new File(Resource.getCurrPath() + "\\data\\kc.lock");

		try {
			channel = new RandomAccessFile(lockFile, "rw").getChannel();
			lock = channel.tryLock();
			
            if (lock == null) {
                JOptionPane.showMessageDialog(
                		new JFrame(),
                		"Application already running", 
                		"Kybernetes Cup", 
                		JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
            }
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		
		/*
		 * Sprachdatei laden und Hauptprogramm starten
		 */
		final Hashtable<String,String> lngContent = LanguageTools.showDialog();
		
		if(lngContent != null){
			
			SwingUtilities.invokeLater(new Runnable() {
				
				@Override
				public void run() {
					Kybernetes.setLanguageContent(lngContent);
					Kybernetes inst = new Kybernetes();
					inst.setLocationRelativeTo(null);	
					inst.setVisible(true);
					inst.toFront();
				}
			});
		}else{
			appUnlock();
			System.exit(0);
		}

	}
	
	/**
	 * <h3>appUnlock</h3>
	 * 
	 * Hebt die Sperrung der Datei und somit den erneuten Programmaufruf auf.
	 *
	 */
	public static void appUnlock(){
		
        try {
        	// Sperre aufheben
			lock.release();
			
			// Datei schliessen
	        channel.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
}
